import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { getToken, setToken, removeToken } from "@/utils/token";
import { getLogin } from "@/api";

export const useUserStore = defineStore(
  "user",
  {
    persist: true,
    state: () => ({
      token: getToken(),
      useraccount:"",
      id:"",
      phonenumber:"",
      username:"",
      useravatar:""  
    }),
    actions: {
      //登录函数
      async login(loginFrom) {
        const ret = await getLogin(loginFrom);
        const newToken = ret.token;
        setToken(newToken);
        this.token = newToken;
        this.useraccount=ret.data.useraccount;
        this.phonenumber=ret.data.phonenumber;
        this.useravatar=ret.data.useravatar
        this.id=ret.data.id
        this.username=ret.data.username
      },
      async getLoginUser() {
        // const ret = await getLoginUser();
        // this.nickName = ret.nickName;
        // this.uid = ret.uid;
        // setToken(ret.token);
      },
      //退出函数
      async clearUserInfo() {
        removeToken();
        this.token = "";
        this.useraccount="";
        this.phonenumber="";
        this.useravatar="";
        this.id=""
      },
    },
  }
);